package com.upu173.music.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.andbase.library.app.base.AbBaseFragment;
import com.andbase.library.utils.AbDialogUtil;
import com.andbase.library.view.recycler.AbRecyclerView;
import com.andbase.library.view.recycler.AbSpaceItemVerticalDecoration;
import com.upu173.music.R;
import com.upu173.music.activity.MusicMainActivity;
import com.upu173.music.adapter.SongFolderRecyclerViewAdapter;
import com.upu173.music.model.Song;
import com.upu173.music.model.SongFolder;
import com.upu173.music.utils.MusicUtil;

import java.util.ArrayList;
import java.util.List;


public class FolderListFragment extends AbBaseFragment {

    private MusicMainActivity activity;
    public List<SongFolder> items = null;
    private SongFolderRecyclerViewAdapter recyclerViewAdapter = null;
    private AbRecyclerView recyclerView = null;
    private FrameLayout rootView = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        rootView = (FrameLayout)inflater.inflate(R.layout.music_fragment_song_list, container, false);

        activity = (MusicMainActivity)this.getActivity();

        items  = new ArrayList<>();

        recyclerViewAdapter = new SongFolderRecyclerViewAdapter(activity,items);

        recyclerView = (AbRecyclerView)rootView.findViewById(R.id.recycler_list);

        recyclerView.addItemDecoration(new AbSpaceItemVerticalDecoration(activity,1));

        recyclerView.setRecyclerViewAdapter(recyclerViewAdapter);

        loadData();

        return rootView;

    }

    @Override
    public void onResume() {
        super.onResume();
    }

    public void loadData(){
        if(items == null){
            return;
        }
        items.clear();
        List<Song> songList = MusicUtil.getSongList(activity);
        if(songList!=null && songList.size() > 0){
            List<SongFolder> songFolderList =  MusicUtil.groupByFolder(songList);
            if(songFolderList!=null && songFolderList.size() > 0){
                items.addAll(songFolderList);
            }
            hideEmptyView(rootView);
        }else{
            showEmptyView(rootView,R.layout.music_view_no_folder_list);

            final Button scanButton = (Button)rootView.findViewById(R.id.scan_button);

            scanButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    scanButton.setText("扫描中...");
                    scanButton.setEnabled(false);
                    activity.scanSong();
                }
            });
        }
        recyclerViewAdapter.notifyDataSetChanged();
    }


}
